package com.zerdoor.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by zhongjieyu on 2014/9/1.
 */
public class Tools {

    /**
     * 获取系统格式化时间字符串
     * @return 时间
     */
    public static String  getFormatSystemTime(){



        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

        String time = sdf.format(new Date());

        return time ;
    }

    /**
     * 获取格式化后的星期
     * @return
     */
    public static String getFormatWeek(){


        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int week = cal.get(Calendar.DAY_OF_WEEK);

        String formatWeek[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

        return formatWeek[week];
    }


    /**
     * 创建文件,如果文件名重复则自动更改文件名
     * @param path 文件路径
     * @param fileName 文件名
     * @param count 默认传入初始次数 2
     * @return  返回新创建的文件
     * @throws Exception
     */
    public static File getFileByOriginalFileName(String path,String fileName,int count) throws Exception{

        File targetFile = new File(path, fileName);


        if(!targetFile.exists()){
            targetFile.mkdirs();
        }else{



            String new_fileName = fileName.substring( 0,fileName.lastIndexOf("."));

            if(count != 2){

                new_fileName = fileName.substring(0,fileName.lastIndexOf("("+(count-1)+")"));


            }

            new_fileName += "(" + count + ")";

            //加入文件后缀
            if(fileName != null && !"".equals(fileName) && fileName.lastIndexOf(".") != -1){

                new_fileName += fileName.substring(fileName.lastIndexOf("."), fileName.length() ).toLowerCase();

            }

            count ++;
            //递归，调用自身
            targetFile = getFileByOriginalFileName(path,new_fileName,count);


        }

        return targetFile;

    }


}
